package org.poptest.processor.annotation;

import org.poptest.annotation.Populate;
import org.poptest.model.Context;
import org.poptest.populate.PopulateContext;
import org.poptest.populate.Populator;
import org.poptest.reflect.ReflectionUtils;
import org.poptest.util.AssertInternal;

import java.lang.annotation.Annotation;

/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @author Alan Roche
 *         Date: 17/02/11
 *         Time: 22:38
 */
public class PopulateProcessor extends AnnotationProcessor<Populate> {
    private final Populator populator;

    public PopulateProcessor(final Populator populator) {
        super(Populate.class);
        this.populator = populator;
    }

    public void process(Context context) {
        AssertInternal.notNull(context.getFieldNode(), "fieldNode is null in context");
        final Populate populateAnnotation = getAnnotation(context.getFieldNode());
        final PopulateContext populateContext = new PopulateContext(
                ReflectionUtils.getValue(context.getTestInstance().getInstance(), context.getFieldNode().getField())) ;
        populator.populate(populateContext);
    }
}
